package com.qigao.mall.web.controller.admin.merchant;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.OrderService;
import com.qigao.mall.api.PromGroupService;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.exceptions.BusinessException;
import com.qigao.mall.commons.utils.excel.ExcelHeaderInfo;
import com.qigao.mall.commons.utils.excel.ExcelUtils;
import com.qigao.mall.web.config.QiGaoProperties;
import com.qigao.mall.web.controller.AbstractExcelController;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.app.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Copyright (C), 2020-2020
 * FileName: AppPromGroupController
 * Author:   luiz
 * Date:     2020/10/15 14:27
 * Description: 团购
 */
@RestController
@RequestMapping("/admin/promGroup")
@Api(tags = "Aadmin团购相关API",value = "Aadmin团购相关API")
public class AdminPromGroupController extends AbstractExcelController {
    private final Logger logger= LoggerFactory.getLogger(getClass());
    @Resource
    PromGroupService promGroupService;
    //创建团购
    @PostMapping("/listPage")
    @ApiOperation(value = "团购分页查询", notes = "团购分页查询")
    public Result<PageInfo<PromGroupResultDto>> listPage(@RequestBody PromGroupCondVo promGroupCondVo, HttpServletRequest request) {
        //权限校验
        ValidatorUtil.validateEntity(promGroupCondVo);
        try {
            PromGroupCondDto promGroupCondDto=new PromGroupCondDto();
            BeanUtils.copyProperties(promGroupCondVo,promGroupCondDto);
           return promGroupService.listPage(promGroupCondDto);
        }catch (Exception e){
            logger.error("团购分页查询失败:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //按照团购编号查询团购详情
    @GetMapping("/findByGroupNo/{groupNo}")
    @ApiOperation(value = "按照团购编号查询团购详情", notes = "按照团购编号查询团购详情")
    public Result<PromGroupResultDto> findByGroupNo(@PathVariable("groupNo")String groupNo) {
        try {
            return promGroupService.findByGroupNo(groupNo);
        }catch (BusinessException be){
            logger.error("按照团购编号查询团购详情失败发生业务异常:{}",be);
            return Result.newError(be.getCode(),be.getMsg());
        }catch (Exception e){
            logger.error("按照团购编号查询团购详情失败:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void exportGet(PromGroupCondVo promGroupCondVo, HttpServletResponse response) {
        PromGroupCondDto promGroupCondDto=new PromGroupCondDto();
        BeanUtils.copyProperties(promGroupCondVo,promGroupCondDto);
        // 待导出数据
        List<PromGroupResultDto> promGroupResultDtos = promGroupService.findByCond(promGroupCondDto);
        ExcelUtils excelUtils = new ExcelUtils(promGroupResultDtos, getHeaderInfo(), null);
        sendHttpResponse(response, "团购报表", excelUtils.getWorkbook());
    }

    // 获取表头信息
    private List<ExcelHeaderInfo> getHeaderInfo() {
        return Arrays.asList(
                new ExcelHeaderInfo(0, 0, 0, 0, "开始时间"),
                new ExcelHeaderInfo(0, 0, 1, 1, "结束时间"),
                new ExcelHeaderInfo(0, 0, 2, 2, "完成时间"),
                new ExcelHeaderInfo(0, 0, 3, 3, "团长"),
                new ExcelHeaderInfo(0, 0, 4, 4, "团长利润"),
                new ExcelHeaderInfo(0, 0, 5, 5, "开团状态"),
                new ExcelHeaderInfo(0, 0, 6, 6, "结算状态"),
                new ExcelHeaderInfo(0, 0, 7, 7, "结算时间"),
                new ExcelHeaderInfo(0, 0, 8, 8, "商品名称")
        );
    }




}
